﻿@{
	Layout = "~/Views/Shared/_Layout.cshtml";
}

@section styles {
	<link href="http://resource.dfancy.cn/components/datepicker/css/datepicker3.css" rel="stylesheet" type="text/css" />
}

<div ng-controller="AddInventoryController">
	<div class="page-header">
		<h3>商品入库</h3>
	</div>

	<div class="panel panel-default">
		<div class="panel-heading">
			<form class="form-inline" ng-submit="findProductList()">
				<div class="input-group">
					<span class="input-group-addon">名称或标签</span>
					<input type="text" class="form-control input-sm" placeholder="请输入商品名称或标签" ng-model="productsSearched.nameOrTag" />
					<span class="input-group-btn">
						<button class="btn btn-info btn-sm" type="submit"><span class="glyphicon glyphicon-search"></span> 查 询</button>
					</span>
				</div>
				<button class="btn btn-info btn-sm" type="button" ng-click="showScanProductModal()"><span class="glyphicon glyphicon-barcode"></span> 扫 码</button>
			</form>
		</div>

		<table class="table table-striped text-center" ng-if="batchDetails.length > 0">
			<thead>
				<tr>
					<th class="text-center">待入库商品条码</th>
					<th class="text-center">待入库商品名称</th>
					<th class="text-center">入库数量</th>
					<th class="text-center">过期日期</th>
					<th class="text-center">操作</th>
				</tr>
			</thead>
			<tbody>
				<tr ng-repeat='detail in batchDetails'>
					<td>{{ detail.ProductBarcode }}</td>
					<td>{{ detail.ProductName }}</td>
					<td>{{ detail.Quantity }} {{ detail.UnitName }}</td>
					<td>{{ detail.ExpiryTime }}</td>
					<td>
						<a href="javascript:;" ng-click="removeDetailFromBatch($index)">删除</a>
					</td>
				</tr>
			</tbody>
		</table>

		<div class="panel-footer text-right" ng-if="batchDetails.length > 0">
			<button type="button" class="btn btn-default btn-sm" ng-click="emptyBatch()"><span class="glyphicon glyphicon-trash"></span> 清 空</button>
			<button type="button" class="btn btn-info btn-sm" ng-click="submitBatch()"><span class="glyphicon glyphicon-inbox"></span> 入 库</button>
		</div>

		<div class="panel-body text-center" ng-if="batchDetails.length == 0">
			<p>尚无待入库商品</p>
		</div>
	</div>

	<div class="modal fade" id="scan_Modal" tabindex="-1" role="dialog" aria-hidden="true">
		<br /><br /><br /><br />
		<div class="modal-dialog modal-sm">
			<div class="modal-content">
				<form ng-submit="findProductByBarcode()">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
						<h4 class="modal-title">读取条码</h4>
					</div>
					<div class="modal-body">
						<input id="txt_barcode" type="text" class="form-control" placeholder="读取条码" ng-model="scanedBarcode" />
					</div>
				</form>
			</div>
		</div>
	</div>

	<div class="modal fade" id="product_list_Modal" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="false">
		<br /><br /><br /><br />
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title">商品列表</h4>
				</div>
				<div class="modal-body form-horizontal">
					<table id="find_product_list" class="table table-striped text-center">
						<thead>
							<tr>
								<th class="text-center">条码</th>
								<th class="text-center">名称</th>
								<th class="text-center">单位</th>
								<th class="text-center">操作</th>
							</tr>
						</thead>
						<tbody>
							<tr ng-repeat='product in productsSearched.products'>
								<td>{{ product.Barcode }}</td>
								<td>{{ product.Name }}</td>
								<td>{{ product.UnitName }}</td>
								<td><a href='javascript:;' ng-click='showBatchDetailSetting(product)'>选择</a></td>
							</tr>
						</tbody>
					</table>
				</div>
			</div>
		</div>
	</div>

	<div class="modal fade" id="not_find_product_Modal" tabindex="-1" role="dialog" aria-hidden="true">
		<br /><br /><br /><br />
		<div class="modal-dialog modal-sm">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title">未找到对应商品</h4>
				</div>
				<div class="modal-body text-center">
					<a href="/Product/Index?barcode={{ scanedBarcode }}" class="btn btn-success">+ 点此添加此商品</a>
				</div>
			</div>
		</div>
	</div>

	<div class="modal fade" id="product_detail_Modal" tabindex="-1" role="dialog" aria-hidden="true" data-backdrop="false">
		<br /><br /><br /><br />
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
					<h4 class="modal-title">商品信息</h4>
				</div>
				<div class="modal-body form-horizontal">
					<div class="form-group">
						<label class="col-md-2 control-label">商品条码</label>
						<div class="col-md-6">
							<p class="form-control-static">{{ tempDetail.product.Barcode }}</p>
						</div>
					</div>
					<div class="form-group">
						<label class="col-md-2 control-label">商品名称</label>
						<div class="col-md-6">
							<p class="form-control-static">{{ tempDetail.product.Name }}</p>
						</div>
					</div>
					<div class="form-group">
						<label class="col-md-2 control-label">入库数量</label>
						<div class="col-md-4">
							<div class="input-group">
								<input type="text" class="form-control" placeholder="请输入入库数量" ng-model="tempDetail.quantity" ng-change="quantityChanged()" />
								<span class="input-group-addon">{{ tempDetail.product.UnitName }}</span>
							</div>
						</div>
					</div>
					<div class="form-group">
						<label class="col-md-2 control-label">过期时间</label>
						<div class="col-md-4">
							<div id="datepicker" class="input-group date">
								<input type="text" class="form-control" ng-model="tempDetail.expiryTime" />
								<span class="input-group-addon">
									<i class="glyphicon glyphicon-th"></i>
								</span>
							</div>
						</div>
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" ng-click='addDetailIntoBatch()'>添 加</button>
				</div>
			</div>
		</div>
	</div>
</div>

@section scripts {
	<script src="http://resource.dfancy.cn/components/datepicker/js/bootstrap-datepicker.js" type="text/javascript"></script>
	<script src="http://resource.dfancy.cn/components/datepicker/js/locales/bootstrap-datepicker.zh-CN.js" type="text/javascript"></script>

	<script type="text/javascript">
		$(document).ready(function () {
			set_left_nav('#ln_AddInventory');
		});

		$('#scan_Modal').on('shown.bs.modal', function (e) {
			$('#txt_barcode').focus();
		});

		$('#product_detail_Modal').on('shown.bs.modal', function (e) {
			$('#txt_product_quantity').focus();
			$('#datepicker').datepicker({
				format: "yyyy-mm-dd",
				startDate: "@DateTime.Today.AddDays(1).ToString("yyyy-MM-dd")",
				language: "zh-CN",
				autoclose: true
			});
		});

		angular.module('dpet').controller('AddInventoryController', function ($scope, $http, $cookieStore) {
			var cookieName = "@ViewBag.CookieName";

			$scope.batchDetails = $cookieStore.get(cookieName) || [];
			$scope.productsSearched = { nameOrTag: '', products: [] };
			$scope.scanedBarcode = '';
			$scope.tempDetail = {
				product: {},
				quantity: 1,
				expiryTime: "@DateTime.Today.AddDays(1).ToString("yyyy-MM-dd")"
			};

			var setBatchIntoCookie = function () {
				$cookieStore.put(cookieName, $scope.batchDetails);
			}

			$scope.findProductList = function () {
				$scope.productsSearched.products = [];

				if ($scope.productsSearched.nameOrTag == '')
					alert('请输入商品名称或商品标签');
				else {
					$http.post('/Ajax/ProductList',
						{
							'NameOrTag': $scope.productsSearched.nameOrTag
						})
						.success(function (products) {
							if (products == null || products.length == 0) {
								alert("未找到对应商品");
								$scope.productsSearched.nameOrTag = '';
							}
							else {
								$scope.productsSearched.products = products;

								$('#product_list_Modal').modal('show');
							}
						})
						.error(function (result) {
							alert("系统繁忙");
						});
				}
			}

			//弹出扫码层
			$scope.showScanProductModal = function () {
				$scope.scanedBarcode = '';

				$('#scan_Modal').modal('show');
			}

			$scope.findProductByBarcode = function () {
				if ($scope.scanedBarcode == '') {
					alert('请读取条码');
					$("#txt_barcode").focus();
				}
				else {
					$('#scan_Modal').modal('hide');

					$http.post('/Ajax/ProductList',
						{
							'Barcode': $scope.scanedBarcode
						})
						.success(function (products) {
							if (products == null || products.length == 0)
								$('#not_find_product_Modal').modal('show');
							else
								$scope.showBatchDetailSetting(products[0]);
						})
						.error(function (result) {
							alert("系统繁忙");
						});
				}
			}

			//弹出入库条目配置层
			$scope.showBatchDetailSetting = function (product) {
				$scope.tempDetail.product = product;

				$('#product_list_Modal').modal('hide');
				$('#product_detail_Modal').modal('show');
			}

			//将入库条目加入入库清单
			$scope.addDetailIntoBatch = function () {
				if ($scope.tempDetail.quantity < 1)
					alert('请输入正确入库数量');
				else if (!/([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8])))/.test($scope.tempDetail.expiryTime))
					alert('请输入正确过期时间');
				else {
					$scope.batchDetails.push({
						ProductId: $scope.tempDetail.product.Id,
						ProductBarcode: $scope.tempDetail.product.Barcode,
						ProductName: $scope.tempDetail.product.Name,
						UnitName: $scope.tempDetail.product.UnitName,
						Quantity: $scope.tempDetail.quantity,
						ExpiryTime: $scope.tempDetail.expiryTime,
					});

					$scope.tempDetail.product = {};
					$scope.tempDetail.quantity = 1;
					$scope.tempDetail.expiryTime = '@DateTime.Today.AddDays(1).ToString("yyyy-MM-dd")';

					$('#product_detail_Modal').modal('hide');
				}
			}

			//删除入库条目
			$scope.removeDetailFromBatch = function (index) {
				$scope.batchDetails.splice(index, 1);
			}

			//清空入库清单
			$scope.emptyBatch = function () {
				if (confirm("是否要清空当前入库清单?")) {
					$scope.productsSearched.nameOrTag = '';
					$scope.batchDetails = [];
				}
			}

			//数量变更检测
			$scope.quantityChanged = function () {
				if (isNaN($scope.quantity) || $scope.quantity < 1)
					$scope.quantity = 1;
			}

			//提交入库批次
			$scope.submitBatch = function () {
				if ($scope.batchDetails.length > 0 && confirm("确认将该批次入库?")) {
					$http.post('/Inventory/AddInventory',
						{
							'details': $scope.batchDetails
						})
						.success(function (result) {
							alert(result.Message);

							if (result.Success) {
								$scope.batchDetails = [];
								window.location.href = "/Inventory/InventoryBatchRecords";
							}
						})
						.error(function (result) {
							alert("系统繁忙");
						});
				}
			}

			$scope.$watch('batchDetails', setBatchIntoCookie, true);
		});

	</script>
}
